package com.ruoyi.weeklyReport.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.engineering.domain.Engineering;
import com.ruoyi.engineering.mapper.EngineeringMapper;
import com.ruoyi.instrument.domain.Instrument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weeklyReport.mapper.WeeklyReportMapper;
import com.ruoyi.weeklyReport.domain.WeeklyReport;
import com.ruoyi.weeklyReport.service.IWeeklyReportService;

/**
 * 周报简报Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-09
 */
@Service
public class WeeklyReportServiceImpl implements IWeeklyReportService
{
    @Autowired
    private WeeklyReportMapper weeklyReportMapper;

    @Autowired
    private EngineeringMapper engineeringMapper;

    /**
     * 查询周报简报
     *
     * @param id 周报简报主键
     * @return 周报简报
     */
    @Override
    public WeeklyReport selectWeeklyReportById(Long id)
    {
        return weeklyReportMapper.selectWeeklyReportById(id);
    }

    /**
     * 查询周报简报列表
     *
     * @param weeklyReport 周报简报
     * @return 周报简报
     */
    @Override
    public List<WeeklyReport> selectWeeklyReportList(WeeklyReport weeklyReport)
    {
        return weeklyReportMapper.selectWeeklyReportList(weeklyReport);
    }

    /**
     * 新增周报简报
     *
     * @param weeklyReport 周报简报
     * @return 结果
     */
    @Override
    public int insertWeeklyReport(WeeklyReport weeklyReport)
    {
        weeklyReport.setCreateTime(DateUtils.getNowDate());
        return weeklyReportMapper.insertWeeklyReport(weeklyReport);
    }

    /**
     * 修改周报简报
     *
     * @param weeklyReport 周报简报
     * @return 结果
     */
    @Override
    public int updateWeeklyReport(WeeklyReport weeklyReport)
    {
        weeklyReport.setUpdateTime(DateUtils.getNowDate());
        return weeklyReportMapper.updateWeeklyReport(weeklyReport);
    }

    /**
     * 批量删除周报简报
     *
     * @param ids 需要删除的周报简报主键
     * @return 结果
     */
    @Override
    public int deleteWeeklyReportByIds(Long[] ids)
    {
        return weeklyReportMapper.deleteWeeklyReportByIds(ids);
    }


    /**
     * 批量逻辑删除周报简报
     *
     * @param ids 需要删除的周报简报主键
     * @return 结果
     */
    @Override
    public int logicDeleteWeeklyReport(Long[] ids,String delType,Long delUser)
    {
        return weeklyReportMapper.logicDeleteWeeklyReport(ids,delType,delUser);
    }

    /**
     * 删除周报简报信息
     *
     * @param id 周报简报主键
     * @return 结果
     */
    @Override
    public int deleteWeeklyReportById(Long id)
    {
        return weeklyReportMapper.deleteWeeklyReportById(id);
    }


    /**
     * 导入用户数据
     *
     * @param weeklyReportList 周报数据列表
     * @return 结果
     */
    @Override
    public String importWeeklyReport(List<WeeklyReport> weeklyReportList)
    {
        if (StringUtils.isEmpty(weeklyReportList))
        {
            throw new ServiceException("导入周报信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WeeklyReport weeklyReport : weeklyReportList) {
            if(weeklyReport != null  && StringUtils.isNotEmpty(weeklyReport.getEngineeringName())){
                Engineering engineering = engineeringMapper.selectEngineeringByName(weeklyReport.getEngineeringName());
                if(engineering != null){
                    weeklyReport.setEngineeringId(engineering.getId());
                    BigDecimal decimal =new BigDecimal(weeklyReport.getDoneTotal());
                    decimal = decimal.setScale(2, RoundingMode.UP); // 向上取整到两位小数
                    decimal = decimal.movePointRight(2);
                    weeklyReport.setDoneTotal(decimal + "%");
                    try {
                        this.insertWeeklyReport(weeklyReport);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、周报 " + weeklyReport.getEngineeringName()+ " 导入成功");
                    }catch (Exception e) {
                        failureNum++;
                        String msg = "<br/>" + failureNum + "、周报 " + weeklyReport.getEngineeringName() + " 导入失败：";
                        failureMsg.append(msg + e.getMessage());
                    }
                }else{
                    failureNum ++;
                    String msg = "<br/>" + failureNum + "、周报 " + weeklyReport + " 导入失败：";
                    failureMsg.append("工程不存在");
                }
            }else{
                failureNum ++;
                String msg = "<br/>" + failureNum + "、周报 " + weeklyReport + " 导入失败：";
                failureMsg.append("数据为空");
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
